package prober;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import queries.Query;
import queries.DocumentSample;

/**
 * Associates a category name against an array of queries.
 * 
 * @author Vika
 * @author ak2765
 */
public class Category {
    
    public Category(String name, Query... qs){
    	children = new ArrayList<Category>();
    	categoryName = name;
        queries = qs.clone();
        docSample = new DocumentSample();
    }
    
    public boolean isLeaf(){
    	return children.isEmpty();
    }
    
    public List<Category> getChildren(){
    	return children;
    }
    
    public void addChild(Category child){
    	children.add(child);
    }
    
    public String getCategoryName(){
    	return categoryName;
    }
    
    public Query[] getQueries(){
    	return queries;
    }
    
	public Query[] getQueriesAndChildrenQueries() {
		List<Query> queries = new LinkedList<Query>();
		queries.addAll(Arrays.asList(this.queries));

		for(Category child : this.children) {
			recusivelyCollectQueries(child, queries);
		}
		
		return queries.toArray(new Query[queries.size()]);
	}

    public void setDocSample(DocumentSample sample){
    	this.docSample = sample;
    }
    
    public DocumentSample getSample() {
    	return this.docSample;
    }
	
    private void recusivelyCollectQueries(Category child, List<Query> queries) {
    	queries.addAll(Arrays.asList(child.queries));
    	
		for(Category childOfTheChild : child.getChildren()) {
			recusivelyCollectQueries(childOfTheChild, queries);
		}
	}

	private String categoryName;
    private Query[] queries;
    private List<Category> children;
    private DocumentSample docSample;
    
	@Override
	public String toString() {
		//return "Category [categoryName=" + categoryName + ", queries="
			//	+ Arrays.toString(queries) + ", children=" + children + "]";
		return "Category [" + categoryName + "]" ;
	//, Documents: \n" + docSample; 
	}
}